<?php
namespace admin\module\publicClass;
use inc\BaseAjax\BaseAjax;
require('../../../global.php');



class ajax extends BaseAjax
{
    
    
    /**
     * 列出所有的公开课
     * @date : ${date} ${time}
     * @author : bapijun
     * @param :  search 搜索的名称 sort 排序方向 asc正向排序 desc逆向排序
     * echo 对应的公开课课程
     */
    protected function listClass()
    {
        global $db;
        if (checkVar($_GET['search'])) {
            $where['AND']['name[~]'] = $_GET['search'];
        } 
        if (checkVar($_GET['sort']) && checkVar($_GET['order'])) {
            $where['ORDER'][$_GET['sort']] = strtoupper($_GET['order']);
        } else {
            $where['ORDER']['tbid'] = 'DESC';
        }
        $where['AND']['type'] = FORMAL_CLASS;
        $echo['total'] = $db->count('tb_class', '*', $where);
        $where['LIMIT'] = array($_GET['offset'], $_GET['limit']);
        $echo['rows'] = array();
        $row = $db->select('tb_class', '*', $where);
        foreach ($row as $value) {
            $tmp['name'] = $value['name'];
            $tmp['number'] = $value['number'];
            $tmp['mobile'] = $value['mobile'];
            $tmp['class_time'] = $value['class_time'];
            $tmp['createtime'] = $value['createtime'];
            $tmp['subsist'] = pennyToYuan($value['subsist']);
            $tmp['price'] = pennyToYuan($value['price']);
            if(checkAdminAuthorize('publicClass.edit')){
                 $tmp['do'] .= ' <a href="javascript:;" class="btn btn-primary btn-xs edit" data-id="'.$value['tbid'].'">编辑</a> ';
            }
            $tmp['do'] .= ' <a href="javascript:;" class="btn btn-primary btn-xs order" data-id="'.$value['tbid'].'">查看订单</a> ';
            $echo['rows'][] = $tmp;
            unset($tmp);
        }
        echo json_encode($echo);
    }

    protected function add()
    {
        global $db;
        $set = array(
            'type' => FORMAL_CLASS,
            'name' => $_POST['name'],
            'address' => $_POST['address'],
            'introduction' => $_POST['introduction'],
            'class_time' => $_POST['class_time'],
            'price' => $_POST['price']*100,
            'subsist' => $_POST['subsist']*100,
            'number' => $_POST['number'],
            'edittime' => date('Y-m-d H:i:s'),
        );
        
        if($_POST['id'] != ''){
        
            $db->update('tb_class', $set, array(
                'tbid' => $_POST['id']
            ));
            $cb['status'] = 'y';
        }else{
        
            $set['createtime'] = date('Y-m-d H:i:s');
            $rs = $db->insert('tb_class', $set);
            if($rs){
                $cb['status'] = 'y';
            }else{
                $cb['status'] = 'n';
            }
        }
        echo json_encode($cb);
    }
    /**
     * 列出所有的公开课订单
     * @author : bapijun
     * @param :  search 搜索的名称 sort 排序方向 asc正向排序 desc逆向排序
     * echo 根据搜索参数返回符合条件的部门或者职位数组
     */
    protected function listOrder()
    {
        global $db;
        if (checkVar($_GET['search'])) {
            $memberWhere['OR']['nickname'] = $_GET['search'];
            $memberWhere['OR']['mobile'] = $_GET['search'];
            $memberWhere['OR']['realname'] = $_GET['search'];
            $memberID = $db->get('tb_member', 'tbid', $memberWhere);
            
            if (!empty($memberID)) {
                $where['AND']['member_id'] = $memberID;
            } else {
                $where['AND']['order_number[~]'] = $_GET['search'];
            }
            
        }
        
        if (checkVar($_GET['begin_time'])) {
            $where['AND']['createtime[>]'] = $_GET['begin_time'];
        }
        if(checkVar($_GET['end_time'])) {
             $where['AND']['createtime[<]'] = date('Y-m-d',strtotime($_GET['end_time'].'+1 day')); 
        }
        if (checkVar($_GET['sort']) && checkVar($_GET['order'])) {
            $where['ORDER'][$_GET['sort']] = strtoupper($_GET['order']);
        } else {
            $where['ORDER']['tbid'] = 'DESC';
        }
         if (checkVar($_GET['cid'])) {
         $where['AND']['class_id'] = $_GET['cid'];
         } 
         if ($_GET['status'] != '') {
             $where['AND']['status'] = $_GET['status'];
         }
        $where['AND']['type'] = FORMAL_CLASS;
        
        $echo['total'] = $db->count('tb_order', '*', $where);
        $where['LIMIT'] = array($_GET['offset'], $_GET['limit']);
        $echo['rows'] = array();
        $row = $db->select('tb_order', '*', $where);
        foreach ($row as $value) {
            $tmp['order_number'] = $value['order_number'];
            $member = $db->get('tb_member', '*', array('tbid' => $value['member_id']));
            $tmp['nickname'] = $member['nickname'];
            $tmp['mobile'] = $member['mobile'];
            $publicClass = $db->get('tb_class', '*', array('tbid' => $value['class_id']));
            $tmp['className'] = $publicClass['name'];
            $tmp['createtime'] = $value['createtime'];
            $tmp['status'] = $this->getStatus($value['status']);
            $tmp['subsist'] = pennyToYuan($value['subsist']);
            $tmp['full_payment'] = pennyToYuan($value['full_payment']);
            if(checkAdminAuthorize('publicClass.payFull') && $value['status'] == ORDER_PAID_SUBSIST){
                $tmp['do'] .= ' <a href="javascript:;" class="btn btn-primary btn-xs payfull" data-id="'.$value['tbid'].'">全额付款完成</a> ';
            }
            if($value['status'] == ORDER_PAID_FULL_PAYMENT){
                $tmp['do'] .= ' <a href="javascript:;" class="btn btn-primary btn-xs schedule" data-id="'.$value['tbid'].'">课程表</a> ';

            }
            if(checkAdminAuthorize('publicClass.finish') && $value['status'] == CLASS_FINISH) {
                $tmp['do'] .= ' <a href="javascript:;" class="btn btn-primary btn-xs finish" data-id="'.$value['tbid'].'">课程完成</a> ';

            }
            $echo['rows'][] = $tmp;
            unset($tmp);
        }
        echo json_encode($echo);
    }
    protected function delete()
    {
        global $db;
        $rs = $db->delete('tb_admin', array('tbid' => $_POST['id']));
        if($rs > 0){
            $cb['status'] = 'y';
        }else{
            $cb['status'] = 'n';
        }
        echo json_encode($cb);
    }
    
    protected function payFull()
    {
        global $db;
        $set = array(
            'status' => ORDER_PAID_FULL_PAYMENT,
            'edittime' => date('Y-m-d H:i:s'),
             'paytime' => date('Y-m-d H:i:s'),
        );
        $rs = $db->update('tb_order', $set, array('tbid' => $_POST['id']));
        if($rs){
            $cb['status'] = 'y';
        }else{
            $cb['status'] = 'n';
        }
        echo json_encode($cb);
    }
    
    protected function finish()
    {
        global $db;
        $set = array(
            'status' => ORDER_FINISH,
            'edittime' => date('Y-m-d H:i:s'),
        );
        $rs = $db->update('tb_order', $set, array('tbid' => $_POST['id']));
        if($rs){
            $cb['status'] = 'y';
        }else{
            $cb['status'] = 'n';
        }
        echo json_encode($cb);
    }
    
    /**
     * 
     * @param tiny类型的状态
     * @return 字符串类型的状态
     * @author : bapijun
     */
    protected function listSchedule()
    {
        global $db;
        $where['tbid'] = $_GET['oid'];
        $order = $db->get('tb_order', '*', $where);
        $class = $db->get('tb_class', '*', array('tbid' => $order['class_id']));
        $member = $db->get('tb_member', '*', array('tbid' => $order['member_id']));
        $echo['total'] = $class['number'];
    
        $shecule = $db->get('tb_schedule', '*', array('order_id' => $_GET['oid']));
        //dumpAndExit($shecule);
        if (empty($shecule)) {
            $scheduleArray = array();
            $scheduleArray = array_pad($scheduleArray, $class['number'], 0);
            $set = array(
                'order_id' => $_GET['oid'],
                'number' => $echo['total'],
                'schedule' => json_encode_ex($scheduleArray),
                'edittime' => date('Y-m-d H:i:s'),
            );
            $db->insert('tb_schedule', $set);
    
        } else {
            $scheduleArray = json_decode($shecule['schedule']);
        }
    
        $echo['rows'] = array();
        for ($i = 0; $i != $echo['total']; $i++) {
            $tmp['lesson'] = '第' . ($i + 1) . '节课';
             
            $tmp['realname'] = $member['realname'];
            $tmp['className'] = $class['name'];
            if ($scheduleArray[$i] == 1) {
                $tmp['status'] = '已上课';
            } else {
                $tmp['status'] = '未上课';
                if(checkAdminAuthorize('publicClass.edit')){
                    $tmp['do'] .= ' <a href="javascript:;" class="btn btn-primary btn-xs edit" data-id="'. $i .'" data-oid="' . $_GET['oid'] . '">上课</a> ';
                }
            }
            $echo['rows'][] = $tmp;
            unset($tmp);
        }
    
        echo json_encode($echo);
    }
    
    /**
     *  将对应的上课更新到对应的数据库中
     * @param 已经上课的id
     * @author : bapijun
     */
    protected function attendClass()
    {
        global $db;
        $schedule = $db->get('tb_schedule', '*', array('order_id' => $_GET['oid']));
        if (empty($schedule)) {
            $cb['status'] = 'n';
        } else {
            $scheduleArray = json_decode($schedule['schedule']);
            $scheduleArray[$_GET['id']] = 1;
            $schedule['schedule'] = json_encode_ex($scheduleArray);
            $schedule['edittime'] = date('Y-m-d H:i:s');
            $db->update('tb_schedule', $schedule, array('order_id'=>$_GET['oid']));
            
            $db->update('tb_schedule', $schedule, array('order_id'=>$_GET['oid']));
            //检查课程是否全部完成
            $IsFinish = $this->checkClassIsFinish($scheduleArray);
            if ($IsFinish) {//如果完成就更新订单的状态
                $orderSet = array(
                    'status' => CLASS_FINISH,
                    'edittime' => date('Y-m-d H:i:s'),
                );
                $db->update('tb_order', $orderSet, array('tbid'=>$_GET['oid']));
            }
            $cb['status'] = 'y';
        }
    
        echo json_encode($cb);
    }
    /**
     * @todo 将对应的int型状态转化为字符串
     * @param tiny类型的状态
     * @return 字符串类型的状态
     * @author : bapijun
     */
    private function getStatus($status)
    {
        /* const ORDER_CLOSE = 0;
        const ORDER_UNPAID = 1;
        const ORDER_PAID_SUBSIST = 2;
        const ORDER_PAID_FULL_PAYMENT = 3;
        const ORDER_FINISH = 4; */
        if ($status == ORDER_CLOSE) {
            return '订单关闭';
        } 
        else if ($status == ORDER_UNPAID) {
            return '未付款';
        }
        else if ($status == ORDER_PAID_SUBSIST) {
            return '已付款';
        } 
        else if ($status == ORDER_PAID_FULL_PAYMENT) {
            return '已付全款';
        } else if ($status == ORDER_FINISH) {
            return '订单完成';
        }
    }
    
    /**
     *  检查当前课程是否结束
     * @param $scheduleArray课程表数组,保存当前是否上课数据
     * @return 结束返回 true,未结束返回false
     * @author bapijun
     */
    private function checkClassIsFinish($scheduleArray)
    {
        $flag = true;
        foreach ($scheduleArray as $value) {
            if ($value == 0) {
                return false;
            }
        }
        return true;
    }

}

$ajax = new ajax();
$ajax->dealWithAjax();
